<?php
class NewsManageController extends AdminController
{
    public function actionView(){
        $page = 1;
        $dataPerpage = 5;
        $newslist = News::getNewslist($page, $dataPerpage);
        $this->render('news', array('newslist' => $newslist, 'num' => 4)); 
    }
    
    public function actionNewsfunc(){
        $this->render('newsfunc');
    }
    
    public function actionChangeStatus() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST" ) {
            $Id = $_POST['newsID'];
            $status = $_POST['status'];
            echo NewsManage::ChangeStatus($Id, $status);
        }
    }
    
    public function actionDeleteNews() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST") {
            $Id = $_POST['NewsID'];
            if( NewsManage::DeleteNews($Id) > 0) {
                $this->redirect("index.php?r=NewsManage/View");
            }
        }
        $this->redirect("index.php?r=NewsManage/View");
    }
    
    public function actionEditNews() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST") {
            if(NewsManage::updateNews($_POST) > 0) {
                $this->redirect("index.php?r=NewsManage/View");
            }
        }
        $this->redirect("index.php?r=NewsManage/View");
    }
    
    public function actionInsertNews() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST") {
            if(NewsManage::InsertNews($_POST) > 0) {
                $this->redirect("index.php?r=NewsManage/View");
            }
        }
        $this->redirect("index.php?r=NewsManage/View");
    }
    
    public function actionGetNews() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST") {
            $newsID = $_POST['newsID'];
            $temp = NewsManage::getNewsDetail($newsID);
            if(isset($temp['NewsID'])) {
                $json = array();
                $json['NewsID']     = $newsID;
                $json['TitleEN']    = $temp['TitleEN'];
                $json['TitleTH']    = $temp['TitleTH'];
                $json['InfoEN']     = $temp['InfoEN'];
                $json['InfoTH']     = $temp['InfoTH'];
                $json['Status']     = $temp['Status'];
                $json['CreateDate'] = $temp['CreateDate'];
                $json['EditDate']   = $temp['EditDate'];
                $json['DeleteDate'] = $temp['DeleteDate'];
                $json = json_encode($json);
                echo $json;
            }
        }
    }
}
?>
